﻿using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using DoeLibsMVC.Models;
using DoeObjects;
using System.Threading.Tasks;
using System.Net;

namespace DoeLibsMVC.Controllers.api
{
    public class ImportController : BaseApiController
    {
        [Security.BasicAuthorize(Roles = Security.UserCategoryHelper.STAFF_CATEGORY)]
        public async Task<HttpResponseMessage> Post([FromBody]string rowJSON)
        {
            JObject row = JsonConvert.DeserializeObject<JObject>(rowJSON);
            JToken token = row["Suggestion"];
            ImportReturn returnValues;

            if (token == null)
            {
                //CSV DATA
                returnValues = await Import.CSVToDB(row, User.UserId);
            }
            else
            {
                //OUR DATA
                returnValues = await Import.SuggestionToDB(row, User.UserId);
            }


            return Request.CreateResponse(HttpStatusCode.OK, returnValues);
        }
    }
}